package huachenxing.mimi520.option.widgets
{
	import com.cambright.lotusroot.Widget;
	import com.cambright.stack.components.AlignmentLayoutComponent;
	import com.cambright.stack.components.ButtonComponent;
	import com.cambright.stack.components.WindowComponent;
	import com.cambright.stack.components.layout.Alignment;
	import com.cambright.stack.components.layout.Direction;
	import com.cambright.stack.components.layout.Element;
	import com.cambright.stack.components.layout.Margin;
	
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import huachenxing.mimi520.option.models.AccountModel;
	import huachenxing.mimi520.resource.TextIcon;
	import huachenxing.mimi520.skin.Button;
	import huachenxing.mimi520.skin.Panel;
	import huachenxing.mimi520.skin.Text;
	
	public class WindowWidget extends Widget
	{
		private var _window:WindowComponent;
		private var _titleLeftLayout:AlignmentLayoutComponent;
		private var _titleRightLayout:AlignmentLayoutComponent;
		private var _closeButtonLayout:AlignmentLayoutComponent;
		private var _closeButton:ButtonComponent;
		private var _contentLayout:AlignmentLayoutComponent;
		
		private var _panel:Panel;
		private var _close:Button;
		
		private var _title:Text;
		
		override protected function invoke():void
		{
			super.invoke();
			
			_panel = new Panel();
			_panel.title.addEventListener(MouseEvent.MOUSE_DOWN, _onTitleDown);
			
			_window = addComponent(WindowComponent);
			_window.rect = rect;
			_window.target = _panel;
			
			_title = new Text(title);
			
			TextIcon.COLOR = 0xFFFFFF;
			TextIcon.SIZE = 24;
			
			_titleLeftLayout = addComponent(AlignmentLayoutComponent);
			_titleLeftLayout.direction = Direction.HORIZONTAL;
			_titleLeftLayout.alignment = Alignment.MIDDLE_LEFT;
			_titleLeftLayout.target = _panel.title;
			_titleLeftLayout.add(new Element(TextIcon.ACCOUNT));
			_titleLeftLayout.add(new Element(_title, 1, null, new Point(0, -1)));
			_titleLeftLayout.enabled = true;
			
			_close = new Button();
			_close.color = 0x1b2f48;
			_close.color_over = 0xe81123;
			_close.color_down = 0xe35662;
			_close.width = _close.height = _panel.title.height;
			
			_closeButton = addComponent(ButtonComponent);
			_closeButton.target = _close;
			_closeButton.clickCommand = close;
			_closeButton.enabled = true;
			
			_closeButtonLayout = addComponent(AlignmentLayoutComponent);
			_closeButtonLayout.direction = Direction.HORIZONTAL;
			_closeButtonLayout.target = _close;
			_closeButtonLayout.enabled = true;
			_closeButtonLayout.add(new Element(TextIcon.CLOSE));
			
			_titleRightLayout = addComponent(AlignmentLayoutComponent);
			_titleRightLayout.direction = Direction.HORIZONTAL;
			_titleRightLayout.alignment = Alignment.MIDDLE_RIGHT;
			_titleRightLayout.target = _panel.title;
			_titleRightLayout.add(new Element(_close));
			_titleRightLayout.enabled = true;
			
			_contentLayout = addComponent(AlignmentLayoutComponent);
			_contentLayout.direction = Direction.VERTICAL;
			_contentLayout.alignment = Alignment.TOP_LEFT;
			_contentLayout.target = _panel.content;
			_contentLayout.margin = new Margin(10, 10, 10, 10);
			_contentLayout.gap = 4;
			_contentLayout.enabled = true;
		}
		
		protected function get rect():Rectangle 
		{ 
			return new Rectangle(Number.MIN_VALUE, Number.MIN_VALUE, 300, 200);
		}
		
		protected function get title():String
		{
			return "未设置";
		}
		
		protected function get close():Class
		{
			throw Error("not implemented");
		}
		
		protected function addElement(element:Element):void
		{
			_contentLayout.add(element);
		}
		
		override protected function enable():void
		{
			_window.enabled = true;
		}
		
		public function activate():void
		{
			_window.activate();
		}
		
		override protected function disable():void
		{
			_window.enabled = false;
		}
		
		private function _onTitleDown(event:MouseEvent):void
		{
			_window.startMove();
		}
	}
}
